package com.hippy.erya.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.entity.RawMaterialDO;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.entity.RawMaterialLossDO;
import com.hippy.erya.mapper.RawMaterialDictionaryMapper;
import com.hippy.erya.mapper.RawMaterialMapper;
import com.hippy.erya.query.BaseQueryWrapper;
import com.hippy.erya.sys.core.constant.cache.Cache;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RawMaterialServiceImpl extends ServiceImpl<RawMaterialMapper, RawMaterialDO> {

    @Autowired
    private RawMaterialMapper rawMaterialMapper;

    @Autowired
    private RawMaterialDictionaryMapper rawMaterialDictionaryMapper;

    @Autowired
    private RawMaterialDictionaryServiceImpl rawMaterialDictionaryService;

    public Page<RawMaterialDO> queryByPage(long currentPage, int limit, String code, String dateRange, String buyChannel) {
        Page<RawMaterialDO> page = new Page<>(currentPage, limit);
        QueryWrapper<RawMaterialDO> queryWrapper = BaseQueryWrapper.buildQuery();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("MATERIAL_CODE", code);
        }
        if (StringUtils.isNotBlank(buyChannel)) {
            queryWrapper.eq("BUY_CHANNEL", buyChannel);
        }
        BaseQueryWrapper.buildQuery(queryWrapper, dateRange, "BUY_DATE");
        Page<RawMaterialDO> pageResult = rawMaterialMapper.selectPage(page, queryWrapper);
        Map<String, String> map = rawMaterialDictionaryService.codeToName(null);
        pageResult.getRecords().forEach(rawMaterialDO -> {
            BaseQueryWrapper.outputMessage(rawMaterialDO);
            String materialCode = rawMaterialDO.getMaterialCode();
            rawMaterialDO.setMaterialName(map.get(materialCode));
        });
        return pageResult;
    }

    public Map<String, RawMaterialDO> queryAll(String userCode) {
        QueryWrapper<RawMaterialDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USABLE_FLAG", Cache.USABLE);
        queryWrapper.orderByDesc("id");
        if (userCode == null) {
            userCode = String.valueOf(LoginContextHolder.getContext().getUser().getId());
        }
        queryWrapper.eq("USER_CODE", userCode);
        List<RawMaterialDO> rawMaterialDOS = rawMaterialMapper.selectList(queryWrapper);
        Map<String, RawMaterialDO> map = new HashMap<>();
        IterableUtils.forEach(rawMaterialDOS, rawMaterialDO -> {
            map.put(rawMaterialDO.getMaterialCode(), rawMaterialDO);
        });
        return map;
    }


    public List<RawMaterialDO> queryByRawMaterialSame(RawMaterialDO rawMaterialDO) {
        QueryWrapper<RawMaterialDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("MATERIAL_CODE", rawMaterialDO.getMaterialCode());
        queryWrapper.eq("BUY_DATE", rawMaterialDO.getBuyDate());
        queryWrapper.eq("BUY_CHANNEL", rawMaterialDO.getBuyChannel());
        return rawMaterialMapper.selectList(queryWrapper);
    }

    public RawMaterialDictionaryDO queryNameByRawMaterialCode(String materialCode) {
        QueryWrapper<RawMaterialDictionaryDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("CODE", materialCode);
        return rawMaterialDictionaryMapper.selectOne(queryWrapper);
    }

    public RawMaterialDO queryByDate(String materialCode, Date date) {
        QueryWrapper<RawMaterialDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("MATERIAL_CODE", materialCode);
        queryWrapper.ge("BUY_DATE", date);
        List<RawMaterialDO> rawMaterialDOS = rawMaterialMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(rawMaterialDOS)) {
            return null;
        }
        return rawMaterialDOS.get(0);
    }


}
